// pages/index/h5.js
Page({

  /**
   * 页面的初始数据 showH5: 初始值必须是false 否则bug
   */
  data: {
    
    h5Url:"http://localhost:9483/#/",
    shortLink:"",
    showH5: false,
    app: null,
    channelId:null,
    isHide:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    
      console.log('h5页面 onLoad: function options=')
      console.log(options)
      var url = options.h5Url
      var shortLink = options.shortLink
      var showH5 = `${options.showH5}` !== '0'
      console.log(`showH5=${showH5}`)
     
      var h5Url = !url ? that.data.h5Url: decodeURIComponent(url)
      var params  =  this.analysisUrlParam(h5Url)
      var channelId = params.channelId
      that.setData(
        {
          h5Url: h5Url,
          showH5:showH5,
          shortLink:shortLink,
          channelId:channelId,
          app: getApp()
        }
      )
     
      console.log('that.data=')
      console.log(that.data)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log('on show')
 
    if(this.data.isHide){
     
   
    this.setData(
      {
        isHide:false,
        showH5:true

       
      }
    )
 
  }
    wx.hideHomeButton()
    },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
      console.log('onHide')
      this.setData({
        isHide:true
      })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (options) {
    var webViewUrl = options.webViewUrl;
    var webViewUrlParams = this.analysisUrlParam(webViewUrl)
    var webViewUrlChannelId = webViewUrlParams.channelId
    var pa = 'cooperation=1'
    if (!webViewUrlChannelId && this.data.channelId){
       pa  = pa+ '&channelId='+this.data.channelId
    }
    if(webViewUrl.indexOf('?')!=-1){
      webViewUrl = webViewUrl + '&'+pa
    }else{
      webViewUrl = webViewUrl + '?'+pa
    }
    var path = '/pages/index/index?showH5=1&h5Url='+encodeURIComponent(webViewUrl)
    console.log('path='+path)
    return {
      title: '嗨耍成都',
      path: path
    }
  },
onTapBack: function(){
  this.setData(
    {
      showH5:true
    }
  )
},
onTap:function (){
  console.log('onTap')
  var that=this
  console.log(that.data.shortLink)
  
  wx.navigateToMiniProgram({
    shortLink:that.data.shortLink,
    envVersion: 'release',
    
    success(res) {
      console.log('跳转支付成功'+new Date())
      that.setData(
        {
          showH5:false
        }
      )
      // 打开成功
    }

  })

},


binderror:function (e){
  
  var that = this
  console.log('binderror')
  console.log(e)

  console.log(JSON.stringify(e))
   if(e.detail.url==='https://wxaurl.cn' || 1==1){
   
  }

},
bindmessage:function (e){
 
  var that = this
  console.log("bindmessage:......")
  console.log(e)
  var data = e.detail.data[0]
  console.log(data)
  var type = data.type
  console.log('type='+type)
  if(type==='pay')
  { 
    var payId = data.payId
   var redirectUrl = data.redirectUrl
   wx.showLoading({
    title: '加载中',
  })
   wx.request({
    url: 'https://open-pay.028wlkj.com:31007/pay/wechat/shortLink',
    method: 'GET',
    data: {
      pageUrl:'pages/index/index?payId='+payId,
      type:'pay'
    },
    success: function (res) {
      wx.hideLoading()
      console.log('请求成功 request resp=');
      console.log(res.data);
      var shortLink = res.data.data.link

   
      wx.navigateTo({
        url: `/pages/h5/index?h5Url=${encodeURIComponent(redirectUrl)}&showH5=0&shortLink=${shortLink}`
      })
     
    },
    fail: function () {
      console.log('请求失败');
      wx.hideLoading()
      wx.showToast({
        title: '请求失败',
        icon: 'error',
        duration: 2000
      })
    }
   });}
   else if(type==='navi'){
     var lat = data.lat
     var log = data.log
     var address = data.address
     var redirectUrl = data.redirectUrl
     
    
     wx.navigateTo({
       url: `/pages/h5/index?h5Url=${encodeURIComponent(redirectUrl)}&showH5=1`,
     })
    wx.openLocation({
      latitude:lat,	//纬度
      longitude: log, //经度
      name: "",	//目的地定位名称
      scale: 15,	//缩放比例
      address: address	//导航详细地址
    })
    
   }
  

},
onTapScan:function(){
  wx.scanCode({
    needResult: 0, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
    scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码，默认二者都有
    success: function (res) {
        var result = res.resultStr // 当needResult 为 1 时，扫码返回的结果
        console.log('scanQRCode result=', res)

    }, fail: function (e) {
        console.log('scanQRCode error=', e)
    }
})
},
  
analysisUrlParam: function (url) {

  var queryParts = url.slice(url.indexOf("?") + 1).split('&');
  var params = queryParts.length > 0 ? {} : null;
  queryParts.map(function (item) {
    var a = item.split('=')
    params[a[0]] = a[1]
  })
  return params
},
})